package com.zhong.thesimpleconnection.util;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

import com.hyphenate.EMCallBack;
import com.hyphenate.EMConnectionListener;
import com.hyphenate.EMError;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.hyphenate.util.NetUtils;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by 俊峰 on 2017/3/25.
 */

public class EMClientHelper {

    private EMClientHelper(){}

//    private Map<String, String> FriendsRequest = new HashMap<>();
//
//    public Map<String, String> getFriendsRequest() {
//        return FriendsRequest;
//    }

    public static EMClientHelper getInstance(){
        return new EMClientHelper();
    }

    public void EMClientRegister(final Context context, String name, final String pass){
        int i = name.indexOf("@");
        if (i > 0) {
            name = name.substring(0, i);
        }
        final String finalName = name;
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //环信注册,注册失败会抛出HyphenateException
                    EMClient.getInstance().createAccount(finalName, pass);
                    Log.d("main", "环信注册成功！");
//            Toast.makeText(context, "环信注册成功！！！", Toast.LENGTH_SHORT).show();
                    SharedPreferences account = context.getSharedPreferences("EMPeopleAccount", MODE_PRIVATE);
                    SharedPreferences.Editor edit = account.edit();
                    edit.putString("EMPeopleAccount_Name", finalName);
                    edit.putString("EMPeopleAccount_Pass", pass);
                    edit.apply();
                } catch (HyphenateException e1) {
                    Log.d("main", "环信注册失败！");
//            Toast.makeText(context, "环信注册失败！！！", Toast.LENGTH_SHORT).show();
                    e1.printStackTrace();
                }
            }
        }).start();
    }

//    public void EMClientLogin(final Context context, String name, String pass){
//        int i = name.indexOf("@");
//        name = name.substring(0, i);
//        SharedPreferences account = context.getSharedPreferences("EMPeopleAccount", MODE_PRIVATE);
//        if (!account.contains("EMPeopleAccount_Name") || !account.contains("EMPeopleAccount_Pass")) {
//            EMClientRegister(context, name, pass);
//        }
//        EMClient.getInstance().login(name,pass,new EMCallBack() {//回调
//            @Override
//            public void onSuccess() {
//                EMClient.getInstance().groupManager().loadAllGroups();
//                EMClient.getInstance().chatManager().loadAllConversations();
//                Log.d("main", "登录聊天服务器成功！");
//            }
//
//            @Override
//            public void onProgress(int progress, String status) {
//
//            }
//
//            @Override
//            public void onError(int code, String message) {
//                Log.d("main", "登录聊天服务器失败！");
//            }
//        });
//    }

    public void EMClientLogout(){
        EMClient.getInstance().logout(true, new EMCallBack() {

            @Override
            public void onSuccess() {
                // TODO Auto-generated method stub
                Log.d("main", "退出成功！");
            }

            @Override
            public void onProgress(int progress, String status) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onError(int code, String message) {
                // TODO Auto-generated method stub
                Log.d("main", "退出失败！");
            }
        });
    }

    public void EMClientConnectionListener(Activity activity) {
        //注册一个监听连接状态的listener
        EMClient.getInstance().addConnectionListener(new MyConnectionListener(activity));
    }

    //实现ConnectionListener接口
    private class MyConnectionListener implements EMConnectionListener {

        private Activity mActivity;

        public MyConnectionListener(Activity activity){
            mActivity = activity;
        }

        @Override
        public void onConnected() {
        }

        @Override
        public void onDisconnected(final int error) {
            mActivity.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    if (error == EMError.USER_REMOVED) {
                        // 显示帐号已经被移除
                        Toast.makeText(mActivity, "帐号已经被移除", Toast.LENGTH_SHORT).show();
                    } else if (error == EMError.USER_LOGIN_ANOTHER_DEVICE) {
                        // 显示帐号在其他设备登录
                        Toast.makeText(mActivity, "帐号在其他设备登录", Toast.LENGTH_SHORT).show();
                    } else {
                        if (NetUtils.hasNetwork(mActivity)){
                            //连接不到聊天服务器
                            Toast.makeText(mActivity, "连接不到聊天服务器", Toast.LENGTH_SHORT).show();
                        }
                        else{
                            //当前网络不可用，请检查网络设置
                            Toast.makeText(mActivity, "当前网络不可用，请检查网络设置", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            });
        }
    }

    //加好友
    public void getFriends(final String UserName, final String Reason){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    EMClient.getInstance().contactManager().addContact(UserName, Reason);
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //删除好友
    public void deleteFriends(final String UserName){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    EMClient.getInstance().contactManager().deleteContact(UserName);
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //同意好友请求
    public void agreeFriendsRequest(final String UserName){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    EMClient.getInstance().contactManager().acceptInvitation(UserName);
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //拒绝好友请求
    public void refuseFriendsRequest(final String UserName){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    EMClient.getInstance().contactManager().declineInvitation(UserName);
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

//    //监听好友状态事件
//    public void setListenerAboutFrindsState(){
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                EMClient.getInstance().contactManager().setContactListener(new EMContactListener() {
//
//                    @Override
//                    public void onContactInvited(String username, String reason) {
//                        //收到好友邀请
//                        FriendsRequest.put(username, reason);
//                        Log.d("main", FriendsRequest.toString());
//                    }
//
//                    @Override
//                    public void onFriendRequestAccepted(String s) {
//                        //好友请求被同意
//                    }
//
//                    @Override
//                    public void onFriendRequestDeclined(String s) {
//                        //好友请求被拒绝
//                    }
//
//                    @Override
//                    public void onContactDeleted(String username) {
//                        //被删除时回调此方法
//                    }
//
//                    @Override
//                    public void onContactAdded(String username) {
//                        //增加了联系人时回调此方法
//                    }
//                });
//            }
//        }).start();
//    }

}
